<?php

class GoodsPicModel extends Model{
	public $table = 'goods_pic';
	public function addPics($gid){
		$upload = new upload();
		$picsFile = $upload->upload('pics');
		$img = new image();
		foreach ($picsFile as  $pic) {
			$bigName = $pic['dir'].$pic['filename'].'800x800.'.$pic['ext'];
			$middleName = $pic['dir'].$pic['filename'].'300x300.'.$pic['ext'];
			$smallName = $pic['dir'].$pic['filename'].'150x150.'.$pic['ext'];
			$bigPic = $img->thumb($pic['path'],$bigName,800,800);
			$middlePic = $img->thumb($pic['path'],$middleName,300,300);
			$smallPic = $img->thumb($pic['path'],$smallName,150,150);
			$this->add(array(
				'goods_gid'=>$gid,
				'pic'=>$bigName,
				'small'=>$smallName,
				'medium'=>$middleName
				));
			unlink($pic['path']);
		}
		
		return true;
	}
	/**
	 * 修改商品规格样图时，需要替换掉原来图片
	 * 该方法调用前，需要调用delpics()将原图数据删掉，不然会造成主键重复
	 * @param  [type] $gid  商品id
	 * @param  [type] $pic_id  商品图片表主键
	 * @return [type]      [description]
	 */
	public function savePics($gid,$pic_id){
		$upload = new upload();
		$picsFile = $upload->upload('pics');
		$img = new image();
		foreach ($picsFile as  $pic) {
			$bigName = $pic['dir'].$pic['filename'].'800x800.'.$pic['ext'];
			$middleName = $pic['dir'].$pic['filename'].'300x300.'.$pic['ext'];
			$smallName = $pic['dir'].$pic['filename'].'150x150.'.$pic['ext'];
			$bigPic = $img->thumb($pic['path'],$bigName,800,800);
			$middlePic = $img->thumb($pic['path'],$middleName,300,300);
			$smallPic = $img->thumb($pic['path'],$smallName,150,150);
			$this->add(array(
				'pic_id'=>$pic_id,
				'goods_gid'=>$gid,
				'pic'=>$bigName,
				'small'=>$smallName,
				'medium'=>$middleName
				));
			unlink($pic['path']);
		}
		
		return true;
	}
	public function delPics($picid){

		$pics = $this->where(array('pic_id'=>$picid))->all();
		$big = $pics[0]['pic'];
		$small = $pics[0]['small'];
		$medium = $pics[0]['medium'];
		//删除所选图片的全部缩略图
		unlink($big);
		unlink($small);
		unlink($medium);
		//删除商品图片数据库中的数据
		$this->delete('pic_id='.$picid);
	}
}
?>
